package com.csu.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.csu.common.CommonResponse;
import com.csu.entity.StudentRecord;
import com.csu.service.RecordService;
import com.csu.service.UserService;
import com.csu.vo.StudentRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RecordController {
    @Autowired
    private RecordService recordService;
    @Autowired
    private UserService userService;
    /**
     * 获取自己的奖罚记录
     */
    @GetMapping("/records/{studentId}/1")
    //@SaCheckRole(value = {"student", "parent"}, mode = SaMode.OR)
    public CommonResponse<List<StudentRecordVo>> getStudentRecordByStudentId(@PathVariable("studentId") int studentId){
        List<StudentRecordVo> studentRecordVos = recordService.getRecords(studentId);
        System.out.println(studentRecordVos.size());
        if (studentRecordVos == null)
            return CommonResponse.createForError("你无权限设置，请联系管理员！");
        else  if (!studentRecordVos.isEmpty())
            return CommonResponse.createForSuccess("SUCCESS",studentRecordVos);
        else
            return CommonResponse.createForError("奖罚列表为空！");
    }
    /**
     * 提交赏罚信息
     */
    @PostMapping("/records")
    //@SaCheckRole("teacher")
    public CommonResponse<Object> commitRecord(@RequestBody StudentRecord studentRecord){
        return recordService.commitRecord(studentRecord.getRecordType(), studentRecord.getTeacherId(),
                studentRecord.getStudentId(), studentRecord.getInformation(), studentRecord.getRecordTitle());
    }
    /**
     * 老师获取自己发布的奖罚记录
     */
    @GetMapping("/records/{teacherId}/0")
    //@SaCheckRole("teacher")
    public CommonResponse<List<StudentRecordVo>> getRecordByTeacherId(@PathVariable("teacherId") int teacherId){
        List<StudentRecordVo> studentRecordVos = recordService.getRecordsByTeacherId(teacherId);
        if (!studentRecordVos.isEmpty())
            return CommonResponse.createForSuccess("SUCCESS",studentRecordVos);
        else
            return CommonResponse.createForError("老师发布的奖罚列表为空！");
    }

    /**
     * 删除奖罚记录
     */
    @DeleteMapping("/records/{teacherId}/{recordId}")
    //@SaCheckRole("teacher")
    public CommonResponse<String> deleteRecord(@PathVariable("teacherId") int teacherId,
                                               @PathVariable("recordId") int recordId){
        int deleteCount = recordService.deleteRecords(teacherId,recordId);
        if (deleteCount == 1)
            return CommonResponse.createForSuccess("SUCCESS","删除记录成功!");
        else
            return CommonResponse.createForError("删除记录失败！");
    }
    /**
     * 发布老师评语
     */
    @PostMapping("/comments/{studentId}")
    //@SaCheckRole("teacher")
    public CommonResponse<String> commitRecordOfTeacher(@PathVariable("studentId") int studentId,
                                                        @RequestBody StudentRecord studentRecord){
        System.out.println("studentId"+studentId);
        System.out.println("studentRecord"+studentRecord);
        studentRecord.setStudentId(studentId);
        int commitCount = recordService.commitRecordOfComment(studentRecord);
        if (commitCount == 1)
            return CommonResponse.createForSuccess("SUCCESS","提交成功！");
        else
            return CommonResponse.createForError("提交失败！");
    }
    /**
     * 学生获取个人所有的评语
     */
    @GetMapping("/comments")
    //@SaCheckRole(value = {"student", "parent"}, mode = SaMode.OR)
    public CommonResponse<List<StudentRecordVo>> getAllCommentsOfStudent(@RequestParam("userId") int userId){
        List<StudentRecordVo> studentRecordVos = recordService.getAllComment(userId);
        if (studentRecordVos == null)
            return CommonResponse.createForError("未绑定学生！");
        else if (!studentRecordVos.isEmpty())
            return CommonResponse.createForSuccess("SUCCESS",studentRecordVos);
        else
            return CommonResponse.createForError("无法获取所有的评语！");
    }
    /**
     * 获取最新的评语
     */
    @GetMapping("/comments/1")
    //@SaCheckRole(value = {"student", "parent"}, mode = SaMode.OR)
    public CommonResponse<StudentRecordVo> getLastComment(@RequestParam("userId") int userId){
        StudentRecordVo studentRecordVo = recordService.getLastComment(userId);
        if (studentRecordVo != null)
            return CommonResponse.createForSuccess("SUCCESS",studentRecordVo);
        else
            return CommonResponse.createForError("学生无最新评语！");
    }
    /**
     * 删除评语
     */
    @DeleteMapping("/comments/{recordId}")
    //@SaCheckRole("teacher")
    public CommonResponse<String> deleteComment(@PathVariable("recordId") int recordId){
        int recordCount = recordService.deleteComment(recordId);
        if (recordCount == 1)
            return CommonResponse.createForSuccess("SUCCESS","删除成功！");
        else
            return CommonResponse.createForError("删除失败！");
    }
}
